
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package model;

import java.lang.Thread.State;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author think
 */
public class CommandRunner {


    private static Queue queuedCommands = new LinkedList();
    private static List runingCommands = new LinkedList();
    private static TSRunnnerThread runnerThread = new TSRunnnerThread();
    private static boolean run;


    static {
        runnerThread.start();
        pause();
    }

    public static TSRunnnerThread getRunnerThead() {
        return runnerThread;
    }

    public static void addCommand(Command cmd) {
        queuedCommands.add(cmd);
    }

    public static Queue getQueuedCommands() {
        return queuedCommands;
    }

    public static List getRunningCommands() {
        return runingCommands;
    }

    public static void pause() {
        synchronized (runnerThread) {
            runnerThread.pause = true;
        }
    }

    public static void resume() {
        synchronized (runnerThread) {
            runnerThread.pause = false;
            runnerThread.notify();
        }
    }

    public static boolean isRunning() {
        State state = runnerThread.getState();
        if (state == State.WAITING) {
            return false;
        } else {
            return true;
        }
    }

    public static void main(String[] args) {
//        TSRunner.start();
        if (!runnerThread.pause) {
            System.out.println("running");

        } else {
            System.out.println("stopped");
        }
    }

    static public class TSRunnnerThread extends Thread {

        boolean pause = false;
        private TSRunner parent;

        public TSRunnnerThread() {
            pause = false;
//            run();
        }

        public void run(){
            while (true) {
                try {
                    synchronized (this) {
                        while (pause) {
                            this.wait();
                        }
                        if(getRunningCommands().size()>0){

                        }
                    }
                } catch (InterruptedException ex) {
                    Logger.getLogger(TSRunner.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

    }
}

